From 7196535ec213a32be1cacdf5e3168c2f281c643b Mon Sep 17 00:00:00 2001 From: robertl Date: Tue, 28 Jun 2005 17:20:27 +0000 Subject: [PATCH] Replace all uses of gmtime(foo) + get_tz_offset() with a call to mkgmtime which converts a tmstruct in UTC to a time_t. --- gpsbabel/coastexp.c | 2 +- gpsbabel/gpx.c | 3 +-- gpsbabel/hiketech.c | 2 +- gpsbabel/igc.c | 4 ++-- gpsbabel/magproto.c | 2 +- gpsbabel/nmea.c | 6 +++--- gpsbabel/pathaway.c | 2 +- gpsbabel/pcx.c | 2 +- gpsbabel/psitrex.c | 2 +- gpsbabel/util.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ gpsbabel/vitosmt.c | 2 +- 11 files changed, 62 insertions(+), 14 deletions(-) diff --git a/gpsbabel/coastexp.c b/gpsbabel/coastexp.c index faef555f5..81b2a19eb 100755 --- a/gpsbabel/coastexp.c +++ b/gpsbabel/coastexp.c @@ -268,7 +268,7 @@ ce_cdata(void *dta, const XML_Char *s, int len) strncpy(secString, currentMark->created+13, 2); secString[2] = '\0'; t.tm_sec = atoi(secString); - currentMark->wp->creation_time = mktime(&t) + get_tz_offset(); + currentMark->wp->creation_time = mkgmtime(&t); } } else if (inRoute) { diff --git a/gpsbabel/gpx.c b/gpsbabel/gpx.c index 178403c9c..1f0508585 100644 --- a/gpsbabel/gpx.c +++ b/gpsbabel/gpx.c @@ -614,8 +614,7 @@ xml_parse_time( const char *cdatastr ) tm.tm_year -= 1900; tm.tm_isdst = 0; - rv = mktime(&tm) + get_tz_offset() - off_sign*off_hr*3600 - - off_sign*off_min*60; + rv = mkgmtime(&tm) - off_sign*off_hr*3600 - off_sign*off_min*60; xfree(timestr); diff --git a/gpsbabel/hiketech.c b/gpsbabel/hiketech.c index a1d026485..b95828246 100644 --- a/gpsbabel/hiketech.c +++ b/gpsbabel/hiketech.c @@ -244,7 +244,7 @@ void ht_trk_utc(const char *args, const char **unused) tm.tm_year -= 1900; tm.tm_isdst = 0; - utc = mktime(&tm) + get_tz_offset() ; + utc = mkgmtime(&tm); wpt_tmp->creation_time = utc; } diff --git a/gpsbabel/igc.c b/gpsbabel/igc.c index 188e7d378..1874e4edb 100644 --- a/gpsbabel/igc.c +++ b/gpsbabel/igc.c @@ -165,7 +165,7 @@ static void igc_task_rec(const char *rec) tm.tm_year += 100; } tm.tm_isdst = 0; - creation = mktime(&tm) + get_tz_offset(); + creation = mkgmtime(&tm); // Create a route to store the task data in. rte_head = route_head_alloc(); @@ -297,7 +297,7 @@ static void data_read(void) tm.tm_year += 100; } tm.tm_isdst = 0; - date = mktime(&tm) + get_tz_offset(); + date = mkgmtime(&tm); } else { // Store other header data in the track descriptions if (strlen(trk_desc) < MAXDESCLEN) { diff --git a/gpsbabel/magproto.c b/gpsbabel/magproto.c index 48151b532..4e0898b06 100644 --- a/gpsbabel/magproto.c +++ b/gpsbabel/magproto.c @@ -915,7 +915,7 @@ mag_trkparse(char *trkmsg) dmy = dmy / 100; tm.tm_mday = dmy % 100; - waypt->creation_time = mktime(&tm) + get_tz_offset(); + waypt->creation_time = mkgmtime(&tm); waypt->centiseconds = fracsecs; if (latdir == 'S') latdeg = -latdeg; diff --git a/gpsbabel/nmea.c b/gpsbabel/nmea.c index 30d9ccf3a..5b00d7b75 100644 --- a/gpsbabel/nmea.c +++ b/gpsbabel/nmea.c @@ -268,7 +268,7 @@ gprmc_parse(char *ibuf) tm.tm_mon = dmy % 100 - 1; dmy = dmy / 100; tm.tm_mday = dmy; - creation_time = mktime(&tm) + get_tz_offset(); + creation_time = mkgmtime(&tm); if (posn_type == gpgga) return; @@ -327,7 +327,7 @@ gpzda_parse(char *ibuf) tm.tm_mday = dd; tm.tm_mon = mm - 1; tm.tm_year = yy - 1900; - creation_time = mktime(&tm) + get_tz_offset(); + creation_time = mkgmtime(&tm); } static void @@ -338,7 +338,7 @@ nmea_read(void) int ckval, ckcmp; struct tm tm; - creation_time = mktime(&tm) + get_tz_offset() + current_time(); + creation_time = mkgmtime(&tm) + current_time(); while (fgets(ibuf, sizeof(ibuf), file_in)) { ck = strrchr(ibuf, '*'); diff --git a/gpsbabel/pathaway.c b/gpsbabel/pathaway.c index ea1fd2a1d..fd779078c 100644 --- a/gpsbabel/pathaway.c +++ b/gpsbabel/pathaway.c @@ -370,7 +370,7 @@ static int ppdb_read_wpt(const struct pdb *pdb_in, const struct pdb_record *pdb_ { tm.tm_year -= 1900; tm.tm_mon--; - wpt_tmp->creation_time = mktime(&tm) + get_tz_offset(); + wpt_tmp->creation_time = mkgmtime(&tm); } break; case 5: diff --git a/gpsbabel/pcx.c b/gpsbabel/pcx.c index fb4f87276..1bafae196 100644 --- a/gpsbabel/pcx.c +++ b/gpsbabel/pcx.c @@ -149,7 +149,7 @@ data_read(void) tm.tm_mon = month_lookup(month); tm.tm_year = atoi(date + 7) + 100; wpt_tmp = waypt_new(); - wpt_tmp->creation_time = mktime(&tm) + get_tz_offset(); + wpt_tmp->creation_time = mkgmtime(&tm); wpt_tmp->latitude = lat; wpt_tmp->longitude = lon; wpt_tmp->altitude = alt; diff --git a/gpsbabel/psitrex.c b/gpsbabel/psitrex.c index 89ab8dd9a..4b86b6ca1 100755 --- a/gpsbabel/psitrex.c +++ b/gpsbabel/psitrex.c @@ -570,7 +570,7 @@ psit_track_r(FILE *psit_file, route_head **trk) &(tmTime.tm_sec)); tmTime.tm_isdst = 0; - dateTime = mktime(&tmTime) + get_tz_offset(); + dateTime = mkgmtime(&tmTime); psit_getToken(psit_file,psit_current_token,sizeof(psit_current_token), whitespace); diff --git a/gpsbabel/util.c b/gpsbabel/util.c index d3a958284..cb9a689df 100644 --- a/gpsbabel/util.c +++ b/gpsbabel/util.c @@ -477,6 +477,7 @@ si_round( double d ) /* * Return a time_t suitable for adding to a time_t that is in GMT to * make it a local time. + * Obsolete: to use mkgmtime instead. */ signed int get_tz_offset(void) @@ -491,6 +492,54 @@ get_tz_offset(void) } } +/* + mkgmtime -- convert tm struct in UTC to time_t + + works just like mktime but without all the mucking + around with timezones and daylight savings + + obsoletes get_tz_offset() + + Borrowed from lynx GPL source code + http://lynx.isc.org/release/lynx2-8-5/src/mktime.c + + Written by Philippe De Muyter . +*/ + +time_t +mkgmtime(struct tm *t) +{ + short month, year; + time_t result; + static int m_to_d[12] = + {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; + + month = t->tm_mon; + year = t->tm_year + month / 12 + 1900; + month %= 12; + if (month < 0) + { + year -= 1; + month += 12; + } + result = (year - 1970) * 365 + m_to_d[month]; + if (month <= 1) + year -= 1; + result += (year - 1968) / 4; + result -= (year - 1900) / 100; + result += (year - 1600) / 400; + result += t->tm_mday; + result -= 1; + result *= 24; + result += t->tm_hour; + result *= 60; + result += t->tm_min; + result *= 60; + result += t->tm_sec; + return(result); +} + + /* * A wrapper for time(2) that allows us to "freeze" time for testing. */ diff --git a/gpsbabel/vitosmt.c b/gpsbabel/vitosmt.c index 9f77dd4c1..ceaf9be87 100644 --- a/gpsbabel/vitosmt.c +++ b/gpsbabel/vitosmt.c @@ -162,7 +162,7 @@ vitosmt_read(void) tmStruct.tm_sec =(int)floor(seconds); tmStruct.tm_isdst =-1; - wpt_tmp->creation_time = mktime(&tmStruct) + get_tz_offset(); + wpt_tmp->creation_time = mkgmtime(&tmStruct); wpt_tmp->centiseconds = fmod(100*seconds+0.5,100); snprintf(shortname, sizeof shortname-1 , "WP%04d", ++serial); -- 2.30.2